package com.lazypeople;

import com.lazypeople.util.FileUtils;
// 移除注册表工具类导入 - 这可能需要管理员权限
// import com.lazypeople.util.RegistryUtils;
import com.lazypeople.controller.DownloadController; // 添加此行
import java.util.List; // 添加此行
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import com.lazypeople.view.SplashController; // 添加正确的导入
import javafx.stage.Stage;
import java.util.Arrays; // 添加缺失的导入
import java.util.Date;
import javafx.stage.StageStyle;
import javafx.scene.image.Image;

public class DownloadApp extends Application {
    private static final Logger logger = LoggerFactory.getLogger(DownloadApp.class);

    @Override
    public void start(Stage primaryStage) throws Exception {
        // 先显示启动界面
        Stage splashStage = new Stage();
        FXMLLoader splashLoader = new FXMLLoader(getClass().getResource("/com/lazypeople/view/SplashScreen.fxml"));
        Parent splashRoot = splashLoader.load();
        SplashController splashController = (SplashController) splashLoader.getController();
        splashStage.setScene(new Scene(splashRoot, 400, 300));
        splashStage.setTitle("懒人下载器");
        splashStage.setResizable(false);
        splashStage.initStyle(StageStyle.UNDECORATED); // 添加此行移除边框
        splashStage.show();

        // 模拟加载过程
        new Thread(() -> {
            try {
                for (int i = 0; i <= 100; i++) {
                    Thread.sleep(30);
                    final int progress = i;
                    Platform.runLater(() -> {
                        splashController.updateProgress(progress / 100.0, "加载中...");
                    });
                }

                // 加载完成后显示主界面
                Platform.runLater(() -> {
                    try {
                        splashStage.close();
                        showMainStage(primaryStage);
                    } catch (Exception e) {
                        e.printStackTrace();
                        logger.error("应用启动失败:", e);
                    }
                });
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
    }

    private void showMainStage(Stage primaryStage) throws Exception {
        // 原有的主界面加载逻辑
        FXMLLoader loader = new FXMLLoader(
                getClass().getResource("/com/lazypeople/controller/DownloadController.fxml"));
        Parent root = loader.load();
        DownloadController controller = loader.getController();

        // 获取命令行参数中的URL
        List<String> args = getParameters().getRaw();
        if (!args.isEmpty()) {
            String url = args.get(0);
            if (FileUtils.isValidUrl(url)) {
                controller.setDownloadUrl(url);
            }
        }
        // 设置窗口图标
        Image icon = new Image(getClass().getResourceAsStream("/assets/idiotdownloader.ico"));
        primaryStage.getIcons().add(icon);
        primaryStage.setTitle("懒人下载管理器");
        primaryStage.setScene(new Scene(root, 800, 600));
        primaryStage.show();
    }

    public static void main(String[] args) {
        try {
            logger.info("=== 应用启动于 {} ===", new Date());
            logger.info("Main方法参数: {}", Arrays.toString(args));
            launch(args);
        } catch (Exception e) {
            logger.error("应用启动失败:", e);
            e.printStackTrace();
        }
    }

}